﻿using System;

using DevExpress.Xpo;

using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;
using System.ComponentModel;
using DevExpress.ExpressApp;
using DevExpress.Data.Filtering;
namespace SpeedSCM.Module
{
    [NavigationItem("销售管理")]
    [ImageName("BO_Sale_Item")]
    public class 销售退货单 : 单据
    {
        public 销售退货单(Session session)
            : base(session)
        {

        }
        protected override SeedType SeedType
        {
            get { return SeedType.销售退货单; }
        }

        private 往来单位 _退货单位;
        [Custom("Caption", "退货单位")]
        [RuleRequiredField(null, DefaultContexts.Save, "必须选择退货单位")]
        public 往来单位 退货单位
        {
            get { return _退货单位; }
            set { SetPropertyValue<往来单位>("退货单位", ref _退货单位, value); }
        }

        private 仓库 _收货仓库;
        [Custom("Caption", "收货仓库")]
        [RuleRequiredField(null, DefaultContexts.Save, "必须选择收货仓库")]
        public 仓库 收货仓库
        {
            get { return _收货仓库; }
            set { SetPropertyValue<仓库>("收货仓库", ref _收货仓库, value); }
        }

        private 银行账户 _付款账户;
        [Custom("Caption", "付款账户")]
        [RuleRequiredField(null, DefaultContexts.Save, "必须选择付款账户")]
        public 银行账户 付款账户
        {
            get { return _付款账户; }
            set { SetPropertyValue<银行账户>("付款账户", ref _付款账户, value); }
        }
        private decimal _付款金额;
        [Custom("Caption", "付款金额")]
        [RuleRequiredField]
        public decimal 付款金额
        {
            get { return _付款金额; }
            set { SetPropertyValue<decimal>("付款金额", ref _付款金额, value); }
        }
        [Association("销售退货单-销售退货单明细")]
        public XPCollection<销售退货单明细> 销售退货单明细
        {
            get { return GetCollection<销售退货单明细>("销售退货单明细"); }
        }
        public override bool Excute(IObjectSpace objectSpace)
        {
            foreach (销售退货单明细 item in 销售退货单明细)
            {
                库存 s = objectSpace.FindObject<库存>(CriteriaOperator.Parse("商品名称=? and 仓库名称=?", item.商品名称, 收货仓库));
                if (s != null)
                {
                    s.库存数量 += item.数量;
                    s.销售总价 -= item.金额;
                }
                s.Save();
            }
            objectSpace.CommitChanges();
            return base.Excute(objectSpace);
        }
    }

    [ImageName("BO_Sale_Item")]
    [RuleCombinationOfPropertiesIsUnique(null, DefaultContexts.Save, "销售退货单,商品名称", CustomMessageTemplate = "销售退货单中已经存在该商品")]
    public class 销售退货单明细 : BaseObject
    {
        public 销售退货单明细(Session session)
            : base(session)
        {

        }
        private 销售退货单 _销售退货单;
        [Custom("Caption", "销售退货单")]
        [Association("销售退货单-销售退货单明细")]
        [Custom("AllowEdit", "False")]
        public 销售退货单 销售退货单
        {
            get { return _销售退货单; }
            set { SetPropertyValue<销售退货单>("销售退货单", ref _销售退货单, value); }
        }
        private 商品 _商品名称;
        [Custom("Caption", "商品名称")]
        [RuleRequiredField(null, DefaultContexts.Save, "必须选择商品名称")]
        [Custom("ImmediatePostData", "True")]
        public 商品 商品名称
        {
            get { return _商品名称; }
            set
            {
                if (SetPropertyValue<商品>("商品名称", ref _商品名称, value))
                {
                    销售单价 = value.销售价;
                }
            }
        }
        private decimal _数量;
        [Custom("Caption", "数量")]
        [Custom("EditMask", "n2")]
        [Custom("DisplayFormat", "{0:n2}")]
        [RuleRequiredField]
        public decimal 数量
        {
            get { return _数量; }
            set { SetPropertyValue<decimal>("数量", ref _数量, value); }
        }
        private decimal _销售单价;
        [Custom("Caption", "销售单价")]
        [RuleRequiredField]
        public decimal 销售单价
        {
            get { return _销售单价; }
            set { SetPropertyValue<decimal>("销售单价", ref _销售单价, value); }
        }
        [Custom("Caption", "金额")]
        public decimal 金额
        {
            get { return 数量 * 销售单价; }
        }
        private string _备注;
        [Custom("Caption", "备注")]
        [Size(500)]
        public string 备注
        {
            get { return _备注; }
            set { SetPropertyValue<string>("备注", ref _备注, value); }
        }
    }
}
